<?php
class Calendar_Core{
	
	public $format_month_day_year = 'm/d/Y';
	public $format_month_text_day_year = 'F d Y';
	
    public function __construct($date)
    {
        $format_month_day_year = $date;
    }
    
    //Now
    public function get_year_now()
    {
    	return date("Y");
    }
    
    public function get_month_text($month)
    {
    	return date("F",mktime(0,0,0,$month
						,$this->get_day_now()
						,$this->get_year_now()));   
    }
    
    public function get_month_text_now()
    {
    	return date("F");
    }
    
    public function get_month_now()
    {
		return date("n");
    }
    
    public function get_week_now()
    {
    	return date("W");
    }
    
    public function get_day_now()
    {
    	return date("j");    
    }
    
    //Date
    public function get_day_of_date($date)
    {
    	return date('d',strtotime($date));
    }

	public function get_month_of_date($date)
    {
    	return date('m',strtotime($date));
    }
    
    public function get_month_text_of_date($date)
    {
    	return date('F',strtotime($date));
    }
    
    public function get_year_of_date($date)
    {
    	return date('Y',strtotime($date));
    }
	    
    //Next Previous
    public function get_next_year($year)
    {
    	return $year + 1;  
    }
    
    public function get_next_year_now()
    {
    	return $this->get_year_now() + 1;  
    }
    
    public function get_prev_year($year)
    {
    	return $year - 1;
    }
    
    public function get_prev_year_now()
    {
    	return $this->get_year_now() - 1;
    }
    
    public function get_next_month($month)
    {
    	$month = $month + 1;
    	if($month > 12)
    		$month = 1;
    	return $month;
    }
    
    public function get_next_text_month($month)
    {
    	$next_month = $this->get_next_month($month);
    	
    	return $this->get_month_text($next_month);
    }
    
    public function get_next_month_now()
    {
    	return $this->get_next_month($this->get_month_now());    
    }
    
    public function get_prev_month($month)
    {
    	$month = $month - 1;
    	if($month < 1)
    		$month = 12;
    	return $month;  
    }
    
    public function get_prev_text_month($month)
    {
    	$prev_month = $this->get_prev_month($month);
    	
    	return $this->get_month_text($prev_month);
    }
    
    public function get_prev_month_now()
    {
    	return $this->get_prev_month($this->get_month_now());  
    }
    
    public function get_date_next_day($day,$month,$year)
    {
    	$date = date($this->format_month_day_year,mktime(0,0,0,$month,$day,$year))." +1 day";

		return date('F d Y',strtotime($date));
    }
    
    public function get_day_of_next_day($day,$month,$year)
    {
    	$date = $this->get_date_next_day($day,$month,$year);

		return date('d',strtotime($date));
    }
    
    public function get_month_of_next_day($day,$month,$year)
    {
    	$date = $this->get_date_next_day($day,$month,$year);

		return date('m',strtotime($date));
    }
    
    public function get_month_text_of_next_day($day,$month,$year)
    {
    	$date = $this->get_date_next_day($day,$month,$year);

		return date('F',strtotime($date));
    }
    
    public function get_year_of_next_day($day,$month,$year)
    {
    	$date = $this->get_date_next_day($day,$month,$year);

		return date('Y',strtotime($date));
    }
    
    public function get_date_prev_day($day,$month,$year)
    {
    	$date = date($this->format_month_day_year,mktime(0,0,0,$month,$day,$year))." -1 day";

		return date('F d Y',strtotime($date));
    }
    
    public function get_day_of_prev_day($day,$month,$year)
    {
    	$date = $this->get_date_prev_day($day,$month,$year);

		return date('d',strtotime($date));
    }
    
    public function get_month_of_prev_day($day,$month,$year)
    {
    	$date = $this->get_date_prev_day($day,$month,$year);

		return date('m',strtotime($date));
    }
    
    public function get_month_text_of_prev_day($day,$month,$year)
    {
    	$date = $this->get_date_prev_day($day,$month,$year);

		return date('F',strtotime($date));
    }
    
    public function get_year_of_prev_day($day,$month,$year)
    {
    	$date = $this->get_date_prev_day($day,$month,$year);

		return date('Y',strtotime($date));
    }
    
	public function get_days_in_month($month,$year)
    {
    	return date("t",mktime(0,0,0,$month+1,0,$year));
    }
    
    public function get_first_day_of_month($month,$year)
    {
    	return date("w", mktime(0,0,0,$month-1,1,$year))+1;    
    }
    
    public function get_time_start_month($month='',$year='')
    {
    	if(empty($month)) $month = $this->get_month_now();
    	if(empty($year)) $year = $this->get_year_now();
		return mktime(0,0,0,$month,1,$year);
	}
    
    public function get_time_end_month($month='',$year='')
    {
    	if(empty($month)) $month = $this->get_month_now();
    	if(empty($year)) $year = $this->get_year_now();
		return mktime(0,0,0,$this->get_days_in_month($month,$year),1,$year);
	}
    
    public function get_next_week($day,$month,$year)
    {
    	$date = date($this->format_month_day_year,mktime(0,0,0,$month,$day,$year))." +1 week";

		return date('W',strtotime($date));
    }
    
    public function get_prev_week($day,$month,$year)
    {
    	$date = date($this->format_month_day_year,mktime(0,0,0,$month,$day,$year))." -1 week";

		return date('W',strtotime($date));
    }

    public function get_day_of_week($day, $month, $year, $calendarSystem = 1)
	{
		$day = intval($day);
		$month = intval($month);
		$year = intval($year);
		
	    # Check for valid parameters #
	    if (!is_int($day) || $day < 1 || $day > 31)
	    {
	        printf('Wrong parameter for $day. It must be an integer between 1 and 31.');
	        exit();
	    }
	       
	    if (!is_int($month) || $month < 1 || $month > 12)
	    {
	        printf('Wrong parameter for $month. It must be an integer between 1 and 12.');
	        exit();
	    }
	       
	    if (!is_int($year) || $year < 0)
	    {
	        printf('Wrong parameter for $year. It must be a positive integer.');
	        exit();
	    }
	       
	    if (!checkdate($month, $day, $year))
	    {
	        printf('Invalid date.'.$month.'/'.$day.'/'.$year);
	        exit();
	    }
	       
	    # Algorithm #       
	    if ($month < 3)
	    {
	        $month = $month + 12;
	        $year = $year - 1;
	    }
       
    	return ($day + (2 * $month) + (int) (6 * ($month + 1) / 10) + $year + (int) ($year / 4) - (int) ($year / 100) + (int) ($year / 400) + $calendarSystem) % 7 + 1;
	} 
	
	public function get_date_week_start()
	{
		$week_start = strtotime('-1 Sunday 00:00');
		
		return date($this->format_month_day_year,$week_start);
	}
	
	public function get_date_week_end()
	{
		$week_end = strtotime('+1 Saturday 23:59');
		
		return date($this->format_month_day_year,$week_end);
	}
	
	public function get_date_start_week($day='',$month='',$year='') {
        if (empty($day)) $day = date('d');
        if (empty($month)) $month = date('m');
        if (empty($year)) $year = date('Y');
        $weekday = date('w', mktime(0,0,0,$month,$day,$year));
        $sunday  = $day - $weekday;
        $start_week = date($this->format_month_day_year, mktime(0,0,0,$month,$sunday,$year));
        $end_week   = date($this->format_month_day_year, mktime(0,0,0,$month,$sunday+6,$year));
        if (!empty($start_week)) {
            return $start_week;
        }
        return false;
    }
    
    public function get_date_end_week($day='',$month='',$year='') {
        if (empty($day)) $day = date('d');
        if (empty($month)) $month = date('m');
        if (empty($year)) $year = date('Y');
        $weekday = date('w', mktime(0,0,0,$month,$day,$year));
        $sunday  = $day - $weekday;
        $start_week = date($this->format_month_day_year, mktime(0,0,0,$month,$sunday,$year));
        $end_week   = date($this->format_month_day_year, mktime(0,0,0,$month,$sunday+6,$year));
        if (!empty($end_week)) {
            return $end_week;
        }
        return false;
    }
	
	public function get_time_start_week($day='',$month='',$year='') {
        if (empty($day)) $day = date('d');
        if (empty($month)) $month = date('m');
        if (empty($year)) $year = date('Y');
        $weekday = date('w', mktime(0,0,0,$month,$day,$year));
        $sunday  = $day - $weekday;
        $start_week = mktime(0,0,0,$month,$sunday,$year);
        $end_week   = mktime(0,0,0,$month,$sunday+6,$year);
        if (!empty($start_week)) {
            return $start_week;
        }
        return false;
    }
    
    public function get_time_end_week($day='',$month='',$year='') {
        if (empty($day)) $day = date('d');
        if (empty($month)) $month = date('m');
        if (empty($year)) $year = date('Y');
        $weekday = date('w', mktime(0,0,0,$month,$day,$year));
        $sunday  = $day - $weekday;
        $start_week = mktime(0,0,0,$month,$sunday,$year);
        $end_week   = mktime(0,0,0,$month,$sunday+6,$year);
        if (!empty($end_week)) {
            return $end_week;
        }
        return false;
    }
	
	public function get_date_week_range($day,$month,$year)
	{
        if (empty($day)) $day = date('d');
        if (empty($month)) $month = date('m');
        if (empty($year)) $year = date('Y');
        $weekday = date('w', mktime(0,0,0,$month,$day,$year));
        $sunday  = $day - $weekday;
        for($i=0; $i<7; $i++)
        {
        	$list[$i] = date($this->format_month_text_day_year, mktime(0,0,0,$month,$sunday+$i,$year));
        }
        if (!empty($list)) {
            return $list;
        }
        return false;
	}
	
	public function get_week_of_year($day,$month,$year)
	{
		return date('W',mktime(0,0,0,$month,$day,$year));
	}
	   
    public function get_date_first_day_next_week($day,$month,$year)
    {
    	$date = $this->get_date_end_week($day,$month,$year).' +1 day';
    	
    	return date($this->format_month_text_day_year,strtotime($date));
    }
    
    public function get_day_of_first_day_next_week($day,$month,$year)
    {
    	$date = $this->get_date_first_day_next_week($day,$month,$year);
    	
    	return $this->get_day_of_date($date);
    }
    
    public function get_month_of_first_day_next_week($day,$month,$year)
    {
    	$date = $this->get_date_first_day_next_week($day,$month,$year);
    	
    	return $this->get_month_of_date($date);
    }
    
    public function get_year_of_first_day_next_week($day,$month,$year)
    {
    	$date = $this->get_date_first_day_next_week($day,$month,$year);
    	
    	return $this->get_year_of_date($date);
    }
    
    public function get_date_last_day_prev_week($day,$month,$year)
    {
    	$date = $this->get_date_start_week($day,$month,$year).' -1 day';
    	
    	return date($this->format_month_text_day_year,strtotime($date));
    }
	
	public function get_day_of_last_day_prev_week($day,$month,$year)
    {
    	$date = $this->get_date_last_day_prev_week($day,$month,$year);
    	
    	return $this->get_day_of_date($date);
    }
    
    public function get_month_of_last_day_prev_week($day,$month,$year)
    {
    	$date = $this->get_date_last_day_prev_week($day,$month,$year);
    	
    	return $this->get_month_of_date($date);
    }
    
    public function get_year_of_last_day_prev_week($day,$month,$year)
    {
    	$date = $this->get_date_last_day_prev_week($day,$month,$year);
    	
    	return $this->get_year_of_date($date);
    }
    
	public function get_date($day,$month,$year)
	{
		$mr['day_now'] = $day;
		
		$mr['week_now'] = $this->get_week_of_year($day,$month,$year);
		
		$mr['month_text_now'] = $this->calendar_model->get_month_text_now();
		
		$mr['month_now'] = $month;
		
		$mr['year_now'] = $year;
		
		//day
		$mr['day_of_next_day'] = $this->calendar_model->get_day_of_next_day($day,$month,$year);
		
		$mr['month_of_next_day'] = $this->calendar_model->get_month_of_next_day($day,$month,$year);
		
		$mr['month_text_of_next_day'] = $this->calendar_model->get_month_text_of_next_day($day,$month,$year);
		
		$mr['year_of_next_day'] = $this->calendar_model->get_year_of_next_day($day,$month,$year);
		
		$mr['day_of_prev_day'] = $this->calendar_model->get_day_of_prev_day($day,$month,$year);
		
		$mr['month_of_prev_day'] = $this->calendar_model->get_month_of_prev_day($day,$month,$year);
		
		$mr['month_text_of_prev_day'] = $this->calendar_model->get_month_text_of_prev_day($day,$month,$year);
		
		$mr['year_of_prev_day'] = $this->calendar_model->get_year_of_prev_day($day,$month,$year);
		
		//week
		$mr['next_week'] = $this->get_next_week($day,$month,$year);
		
		$mr['prev_week'] = $this->get_prev_week($day,$month,$year);
		
		$mr['day_of_first_day_next_week'] = $this->get_day_of_first_day_next_week($day,$month,$year);
		
		$mr['month_of_first_day_next_week'] = $this->get_month_of_first_day_next_week($day,$month,$year);
		
		$mr['year_of_first_day_next_week'] = $this->get_year_of_first_day_next_week($day,$month,$year);
		
		$mr['day_of_last_day_prev_week'] = $this->get_day_of_last_day_prev_week($day,$month,$year);
		
		$mr['month_of_last_day_prev_week'] = $this->get_month_of_last_day_prev_week($day,$month,$year);
		
		$mr['year_of_last_day_prev_week'] = $this->get_year_of_last_day_prev_week($day,$month,$year);
		
		//month
		$mr['next_text_month'] = $this->calendar_model->get_next_text_month($month);
		
 		$mr['prev_text_month'] = $this->calendar_model->get_prev_text_month($month);
 		
 		$mr['next_month'] = $this->calendar_model->get_next_month($month);
		
 		$mr['prev_month'] = $this->calendar_model->get_prev_month($month);
 		
 		//year
 		$mr['next_year'] = $this->calendar_model->get_next_year($year);
 		
 		$mr['prev_year'] = $this->calendar_model->get_prev_year($year);
 		
 		return $mr;
	}
	
	public function get_date_now()
	{
		return $this->get_date($this->calendar_model->get_day_now()
						,$this->calendar_model->get_month_now()
						,$this->calendar_model->get_year_now());
	}
	
	public function get_months_of_year()
	{
		for ($i = 0; $i < 12; $i++) {
			
			$month = $i + 1;
			
			$mlist[$i]['num'] = $month;
			
			$mlist[$i]['name'] = $this->get_month_text($month);
			
		}
		return $mlist;
	}
	
	public function get_months_of_year_now()
	{
		return $this->get_months_of_year($this->calendar_model->get_year_now());
	}
	
    public function get_time_start_year($year='')
    {
    	if(empty($year)) $year = $this->get_year_now();
		return mktime(0,0,0,1,1,$year);
	}
    
    public function get_time_end_year($year='')
    {
    	if(empty($year)) $year = $this->get_year_now();
		return mktime(0,0,0,12,30,$year);
	}
    
	public function get_years($year_now, $size = 5)
	{		
		$count = 0;
		
		for ($i = $size; $i >= 1; $i--) {
			$mlist[$count] = $year_now - $i;
			$count++;
		}
		
		$mlist[$count] = $year_now;
		
		$count++;
		
		for ($i = 1; $i <= $size; $i++) {
			$mlist[$count] = $year_now + $i;
			$count++;
		}
		return $mlist;
	}
	
	public function get_years_now()
	{
		$year_now = $this->calendar_model->get_year_now();
		
		return $this->get_years($year_now);
	}
	
	public function get_calendar_month($day_now, $month_now, $year_now)
	{
		$num_weeks = 5;
		
		$days_week = 7;
		
		$day_count = 1;
		
		$days_in_month = $this->calendar_model->get_days_in_month($month_now,$year_now);
		
		for($i=0; $i<$num_weeks; $i++)
		{
			for($j=1; $j<=$days_week; $j++)
			{
				if($day_count <= $days_in_month)
					$day_of_week = $this->calendar_model->get_day_of_week($day_count, $month_now, $year_now);
								
				if(intval($day_of_week) == $j)
				{
					if($j == 1)
						$mlist[$i]['sun'] = $day_count;
					if($j == 2)
						$mlist[$i]['mon'] = $day_count;
					if($j == 3)
						$mlist[$i]['tue'] = $day_count;
					if($j == 4)
						$mlist[$i]['wed'] = $day_count;
					if($j == 5)
						$mlist[$i]['thu'] = $day_count;
					if($j == 6)
						$mlist[$i]['fri'] = $day_count;
					if($j == 7)
						$mlist[$i]['sat'] = $day_count;
					
					$mlist[$i][$j] = $day_count;
					
					$day_count++;
				}
				else
				{
					if($j == 1)
						$mlist[$i]['sun'] = null;
					if($j == 2)
						$mlist[$i]['mon'] = null;
					if($j == 3)
						$mlist[$i]['tue'] = null;
					if($j == 4)
						$mlist[$i]['wed'] = null;
					if($j == 5)
						$mlist[$i]['thu'] = null;
					if($j == 6)
						$mlist[$i]['fri'] = null;
					if($j == 7)
						$mlist[$i]['sat'] = null;
						
					$mlist[$i][$j] = null;				
				}
			}
			$mlist[$i]['week'] = $i+1;
			
			$mlist[$i]['today'] = $day_now;
			
			$mlist[$i]['day_now'] = $day_now;
			
			$mlist[$i]['month_now'] = $month_now;
			
			$mlist[$i]['year_now'] = $year_now;
		}
		return $mlist;
	}
	
	public function get_calendar_now()
	{
		$day_now = $this->calendar_model->get_day_now();
		
		$month_now = $this->calendar_model->get_month_now();
		
		$year_now = $this->calendar_model->get_year_now();
		
		$mlist = $this->calendar_model->get_calendar_month($day_now,$month_now,$year_now);
		
		return $mlist;	
	}
	
	public function get_calendar_day_now()
	{
		$day_now = $this->calendar_model->get_day_now();
		
		$month_now = $this->calendar_model->get_month_now();
		
		$year_now = $this->calendar_model->get_year_now();
		
		for($i=0; $i<12; $i++)
		{
			$mlist[$i] = date('h:i a', mktime($i+7, 0, 0, $day_now, $month_now, $year_now));
		}
		
		return $mlist;
	}
	
	public function get_calendar_week($day, $month, $year)
	{		
		$list_week = $this->calendar_model->get_date_week_range($day,$month,$year);
		
		for($i=0; $i<count($list_week); $i++)
		{
			$day_real = date('d',strtotime($list_week[$i]));
			
			$month_real = date('m',strtotime($list_week[$i]));
			
			$year_real = date('Y',strtotime($list_week[$i]));
			
			$day_of_week = $this->calendar_model->get_day_of_week($day_real, $month_real, $year_real);
			
			if($month_real == $month)
			{
				if($day_of_week == 1)
					$list[0]['sun'] = ' ';
				if($day_of_week == 2)
					$list[0]['mon'] = ' ';
				if($day_of_week == 3)
					$list[0]['tue'] = ' ';
				if($day_of_week == 4)
					$list[0]['wed'] = ' ';
				if($day_of_week == 5)
					$list[0]['thu'] = ' ';
				if($day_of_week == 6)
					$list[0]['fri'] = ' ';
				if($day_of_week == 7)
					$list[0]['sat'] = ' ';
			}
			else
			{
				if($day_of_week == 1)
					$list[0]['sun'] = null;
				if($day_of_week == 2)
					$list[0]['mon'] = null;
				if($day_of_week == 3)
					$list[0]['tue'] = null;
				if($day_of_week == 4)
					$list[0]['wed'] = null;
				if($day_of_week == 5)
					$list[0]['thu'] = null;
				if($day_of_week == 6)
					$list[0]['fri'] = null;
				if($day_of_week == 7)
					$list[0]['sat'] = null;				
			}
			
			$list[0]['today'] = $day;
	
			$list[0]['day_now'] = $day;
			
			$list[0]['month_now'] = $month;
			
			$list[0]['year_now'] = $year;
		}
		return $list;
	}
 }
 ?>